package com.xhh.nphm.gaoxin.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.QueryConditionVo;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.DateUtils;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.gaoxin.entity.GxqTtelGetDealEty;
import com.xhh.nphm.gaoxin.entity.GxqTworkFormBaseEty;
import com.xhh.nphm.gaoxin.entity.GxqTworkFormLogEty;
import com.xhh.nphm.gaoxin.service.GxqITelGetDealService;
import com.xhh.nphm.gaoxin.service.GxqITworkFormLogService;
import com.xhh.nphm.gaoxin.service.GxqIWorkFormBaseService;
import com.xhh.nphm.system.domain.IsGoBackConfig;
import com.xhh.nphm.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@RestController
@RequestMapping("/gaoxin/system/mTelWF")
public class GxqMTelWFrManageController extends BaseController {
    private String prefix = "system/gaoxin/mTelWF";
    @Autowired
    private GxqIWorkFormBaseService gxqWorkFormBaseService;
    @Autowired
    private GxqITworkFormLogService gxqTworkFormLogService;
    @Autowired
    private GxqITelGetDealService gxqTelGetDealService;
    
    @Autowired
    private IsGoBackConfService isGoBackConfService;
    @Autowired
    private ICommonService commonService;

    /**
     * 工单初稿直接答复页面
     */
    @GetMapping("/rManage/tWFormOver/{pkId}")
    public ModelAndView tWFormOver(@PathVariable Integer pkId, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        if (GxqTworkFormBaseEty != null) {
            map.put("workFormBase", GxqTworkFormBaseEty);
        }
        return new ModelAndView(prefix + "/rManage/tWFormOver");
    }

    /**
     * 重新派单 页面
     *
     * @param pkId 工单编号
     * @param map 模型
     * @return 视图
     */
    @GetMapping("/rManage/wfDraftCheck/{pkId}")
    public ModelAndView wfDraftCheck(@PathVariable("pkId") Integer pkId, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        if (GxqTworkFormBaseEty != null) {
            GxqTtelGetDealEty telDeal = gxqTelGetDealService.selectByPkId(GxqTworkFormBaseEty.getGetDealPk());
            map.put("workFormBase", GxqTworkFormBaseEty);
            map.put("telDeal", telDeal);
        }
        return new ModelAndView(prefix+"/rManage/wfDraftCheck");
    }

    /**
     * 工单初稿 直接答复
     */
    @PostMapping("rManage/tWFormOverJson")
    public AjaxResult forcedend(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        SysUser user = ShiroUtils.getSysUser();
        UpdateWrapper<GxqTworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("WsUFlag", 1)
                .set("DoverTime", new Date())
                .set("CrtStepTime", new Date())
                .set("RecordFlag", 29).set("RecordTime", new Date()).set("RecUserNo", user.getUserNo())
                .set("RecUserName", user.getUserName()).set("CrtStepName", "强制结束").set("CrtStepId", 960)
                .set("DOverDesc", GxqTworkFormBaseEty.getDoverDesc())
                .eq("PkId", GxqTworkFormBaseEty.getPkId());

        boolean flag = gxqWorkFormBaseService.update(null, updateWrapper);
        if (flag) {
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTworkFormBaseEty.getPkId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpAction("强制结束");
            etyLog.setOpBtnId(-1);
            etyLog.setWsDesc("结束工单。");
            etyLog.setWsOpen(1);
            etyLog.setOpDesc("强制结束:" + GxqTworkFormBaseEty.getDoverDesc());
            etyLog.setEffectFlag(-1);
            gxqTworkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }

    /**
     * 网评不满意处理页面
     */
    @GetMapping("/rManage/tWFormEdit/{pkId}")
    public ModelAndView tWFormEdit(@PathVariable Integer pkId, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        map.put("workFormBase", GxqTworkFormBaseEty);
        return new ModelAndView(prefix + "/rManage/tWFormEdit");
    }

    /**
     * 网盘不满意修改数据
     */
    @PostMapping("rManage/tWFormEditJson")
    public AjaxResult tWFormEditJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        UpdateWrapper<GxqTworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PkId", GxqTworkFormBaseEty.getPkId())
                .set("fromTelA", GxqTworkFormBaseEty.getFromTelA())
                .set("DCntACode", GxqTworkFormBaseEty.getDcntAcode())
                .set("DCntAName", GxqTworkFormBaseEty.getDcntAname())
                .set("DFmClaNo", GxqTworkFormBaseEty.getDfmClaNo())
                .set("DFmClaName", GxqTworkFormBaseEty.getDfmClaName())
                .set("FromName", GxqTworkFormBaseEty.getFromName())
                .set("FmAreaNo", GxqTworkFormBaseEty.getFmAreaNo())
                .set("FmAreaName", GxqTworkFormBaseEty.getFmAreaName())
                .set("FmAddress", GxqTworkFormBaseEty.getFmAddress())
                .set("WsTopic", GxqTworkFormBaseEty.getWsTopic())
                .set("SetProID", GxqTworkFormBaseEty.getSetProId())
                .set("SetProName", GxqTworkFormBaseEty.getSetProName())
                .set("OpenX", GxqTworkFormBaseEty.getOpenX())
                .set("CrtStepID", GxqTworkFormBaseEty.getCrtStepId())
                .set("CrtStepName", GxqTworkFormBaseEty.getCrtStepName())
                .set("ReportFlag", GxqTworkFormBaseEty.getReportFlag());
        boolean flag = gxqWorkFormBaseService.update(null, updateWrapper);
        if (flag) {
            SysUser user = ShiroUtils.getSysUser();
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTworkFormBaseEty.getPkId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpAction("再次整理");
            etyLog.setOpBtnId(-1);
            etyLog.setWsDesc("再次整理工单");
            etyLog.setWsOpen(1);
            etyLog.setOpDesc("再次整理工单");
            etyLog.setEffectFlag(-1);
            gxqTworkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }


    /**
     * 进入待审办理结果列表页面
     * @author zz
     */
    @GetMapping("/rManage/replyWait")
    public ModelAndView replyWait() {
        return new ModelAndView(prefix + "/rManage/replyWait");
    }

    /**
     * 待审办理结果列表数据
     */
    @PostMapping("rManage/replyWaitJson")
    public TableDataInfo replyWaitJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        startPage();
        List<GxqTworkFormBaseEty> list = gxqWorkFormBaseService.replyWaitJson(GxqTworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入审核办理结果页面
     * @param wfId 工单id
     * @author zz
     */
    @GetMapping("/rManage/chkResultS/{id}")
    public ModelAndView chkResultS(@PathVariable("id")Integer wfId,ModelMap map) {
        map.put("workFormEty",gxqWorkFormBaseService.getById(wfId));
        return new ModelAndView(prefix + "/rManage/chkResultS");
    }

    /**
     * 审核办理结果
     * @param GxqTworkFormBaseEty
     * @return
     */
    @PostMapping("rManage/chkResultS")
    public AjaxResult chkResultS(GxqTworkFormBaseEty GxqTworkFormBaseEty,Integer act) {
        //保存响应结果
        boolean flag = false;
        switch (act) {
            //通过并提交
            case 1:
                //修改工单信息
                UpdateWrapper<GxqTworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("pkId", GxqTworkFormBaseEty.getPkId())
                        .set("doverTime", DateUtils.getNowDate())
                        .set("doverDesc", GxqTworkFormBaseEty.getDoverDesc())
                        .set("wsUFlag", 1)
                        .set("crtStepId", GxqTworkFormBaseEty.getCrtStepId())
                        .set("crtStepName", GxqTworkFormBaseEty.getCrtStepName())
                        .set("crtStepTime",DateUtils.getNowDate());
                flag = gxqWorkFormBaseService.update(updateWrapper);
                //新增工单操作日志
                SysUser user = getSysUser();
                GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
                etyLog.setWfId(GxqTworkFormBaseEty.getPkId());
                etyLog.setOpDeptNo(user.getDeptId().toString());
                etyLog.setOpDeptName(user.getDeptName());
                etyLog.setOpUserNo(user.getUserNo());
                etyLog.setOpUserName(user.getUserName());
                String desc = GxqTworkFormBaseEty.getDoverDesc();
                if (81 == GxqTworkFormBaseEty.getSetProId()) {
                    etyLog.setWsOpen(0);
                    etyLog.setOpAction("提交领导审核");
                    desc = "提交领导审核";
                } else {
                    etyLog.setWsOpen(1);
                    etyLog.setOpAction("提交回访");
                    desc = "审定办理结果。";
                }
                etyLog.setWsDesc(desc);
                etyLog.setOpDesc(desc);
                etyLog.setEffectFlag(1);
                flag = gxqTworkFormLogService.save(etyLog) > 0;
                break;
            default :
                break;
        }
        return toAjax(flag);
    }

    /**
     * 下派退回配置
     * @param map
     * @return
     */
    @GetMapping("/rManage/goBackConfig")
    public ModelAndView goBackConfig(ModelMap map){
        SysUser user = getSysUser();
        IsGoBackConfig isGoBackConfig = isGoBackConfService.selectConfByDept(user.getDeptId());
        if (isGoBackConfig != null){
            // 部分部门
            if (isGoBackConfig.getIsAllow() == 1 && isGoBackConfig.getIsAll() == 101) {
                map.put("noGoBackDeptNames",isGoBackConfService.selectNoGoBackDeptNames(isGoBackConfig.getDeptId()));
            }
        }else{
            //默认允许所有下级部门退回
            isGoBackConfig = new IsGoBackConfig();
            isGoBackConfig.setIsAll(100);
            isGoBackConfig.setIsAllow(0);
        }
        map.put("goBackConfig",isGoBackConfig);
        return new ModelAndView(prefix + "/rManage/goBackConfig");
    }

    /**
     * 选择不允许退回部门
     * @param map
     * @return
     */
    @GetMapping("/rManage/NoGoBackDeptAll")
    public ModelAndView NoGoBackDeptAll(ModelMap map){
        map.put("areaNo", getSysUser().getAreaNo());
        map.put("deptId", getSysUser().getDeptId());
        return new ModelAndView(prefix + "/rManage/NoGoBackDeptAll");
    }


    @PostMapping("/rManage/isGoBackConf")
    public AjaxResult isGoBackConf(IsGoBackConfig isGoBackConfig){
        boolean flag = false;
        SysUser user = ShiroUtils.getSysUser();
        List<String> noGoBackDeptIds = new ArrayList<>();
        isGoBackConfig.setDeptId(user.getDeptId());
        isGoBackConfig.setDeptName(user.getDeptName());
        isGoBackConfig.setOptId(user.getUserId());
        isGoBackConfig.setOptName(user.getUserName());
        isGoBackConfig.setOptTime(new Date());
        if (isGoBackConfService.saveOrUpdateConf(isGoBackConfig,user)) {
            if (isGoBackConfig.getIsAllow() == 1){//不允许退回保存不允许退回的部门
                //所有下级部门100？部分下级部门101？
                if (isGoBackConfig.getIsAll() == 101 && isGoBackConfig.getNoGoBackDeptIds() != null ){
                    noGoBackDeptIds = Arrays.asList(isGoBackConfig.getNoGoBackDeptIds().split(","));
                }else if (isGoBackConfig.getIsAll() == 100){
                    noGoBackDeptIds = commonService.findAllDeptId(user.getAreaNo(),user.getDeptId().toString());
                }
            }//删除
            if (isGoBackConfService.deleteDeptConfByDeptId(user.getDeptId())){
                //保存
                flag = isGoBackConfService.insertDeptConf(user.getDeptId(),noGoBackDeptIds);
            }

        }
        return flag ? AjaxResult.success("配置成功！") : AjaxResult.error("配置失败！");
    }


    /**
     * 电话紧急联系调整 列表页面
     */
    @GetMapping("/rManage/twmBankNameList")
    public ModelAndView twmBankNameList() {
        return new ModelAndView(prefix + "/rManage/twmBankNameList");
    }

    /**
     * 电话紧急联系调整
     */
    @PostMapping("/rManage/twmBankNameListJson")
    public TableDataInfo twmBankNameListJson(QueryConditionVo qc) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.wfListBankName(qc);
        return getDataTable(list);
    }

    /**
     * 电话紧急联系调整 编辑页面
     */
    @GetMapping("/rManage/tWBankName/{pkId}")
    public ModelAndView tWBankName(@PathVariable Integer pkId, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        if (GxqTworkFormBaseEty != null) {
            map.put("workFormBase", GxqTworkFormBaseEty);
        }
        return new ModelAndView(prefix + "/rManage/tWBankName");
    }

    /**
     * 电话紧急联系调整 编辑页面
     */
    @PostMapping("/rManage/tWBankNameSave")
    public AjaxResult tWBankNameSave(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        GxqTworkFormBaseEty GxqTworkFormBaseEtyOld = gxqWorkFormBaseService.getById(GxqTworkFormBaseEty.getPkId());
        GxqTworkFormBaseEtyOld.setMark(GxqTworkFormBaseEty.getMark());
        boolean flag = gxqWorkFormBaseService.updateById(GxqTworkFormBaseEtyOld);
        if(flag) {
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTworkFormBaseEty.getPkId());
            SysUser sysUser = getSysUser();
            etyLog.setOpDeptNo(sysUser.getDeptId().toString());
            etyLog.setOpDeptName(sysUser.getDeptName());
            etyLog.setOpUserNo(sysUser.getUserNo());
            etyLog.setOpUserName(sysUser.getUserName());
            etyLog.setOpAction("修改");
            etyLog.setOpBtnId(-1);
            etyLog.setWsDesc("修改工单信息");
            etyLog.setWsOpen(0);
            etyLog.setOpDesc("修改信息");
            etyLog.setEffectFlag(-1);
            gxqTworkFormLogService.save(etyLog);
        }
        return toAjax(flag, "修改成功");
    }
}
